package com.wthink.newMain.newBusiness.inner

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.wthink.hzgj.R
import com.wthink.hzgj.business.adapter.BusinessAdapter
import com.wthink.hzgj.business.data.Business
import com.wthink.hzgj.databinding.FragmentBusinessInnerUpBinding
import com.wthink.hzgj.databinding.FragmentMsgInnerReaderBinding
import com.wthink.hzgj.util.*
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.newMain.msg.inner.viewmodel.ReadedViewmodel
import com.wthink.newMain.newBusiness.adapter.BusinessUpAdapter
import com.wthink.newMain.newBusiness.data.BusinessData
import com.wthink.newMain.newBusiness.inner.viewmodel.NewBusinessUpViewmodel

class BusinessUpFragment : Fragment(){
    private val viewModel by viewModels<NewBusinessUpViewmodel>()

    private lateinit var binding: FragmentBusinessInnerUpBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_business_inner_up,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initList()
    }
    private fun initList() {
        var array: Array<String> = arrayOf(
            "请求排班", "请求交班", "请求加油", "请求加气", "请求充电", "请求维修", "请求包车", "其他请求","退出运营","手动开始","手动结束"
        )

        val arrayList = ArrayList<String>()
        for (item in array) {
            arrayList.add(item)
        }

        //viewModel.start()
        binding.upRecycle.layoutManager = GridLayoutManager(context, 4)
        binding.upRecycle.adapter = BusinessUpAdapter().apply {
            this.update(arrayList)
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val notFastClick = ClickUtil.isNotFastClick()
                    if (notFastClick) {
                        viewModel.updateBusiness((obj as String)) {
                            val businessData =
                                BusinessData(DpUtil.getDate()!!, DpUtil.getTime()!!, obj as String)
                            PreferencesUtils.saveBusinessUpListToString(businessData)
                            activity?.runOnUiThread {
                                toast(context!!,"请求成功")
                            }
                        }
                    }
                }
            }
        }
        closeDefaultAnimator(binding.upRecycle)
    }
}